<?php

use app\config\Db;
use Core\Result\Result;
use Core\Common\Common;
use Core\Utils\Utils;

/**
 * 扫码充值参数
 */

function ScanRechargeInfo($data)
{
    $qcode = $data['qcode'];
    $sign = $data['sign'];
    $encryption = "S6BZDQDNH73H";
    $encryptedString = MD5(MD5($qcode.$encryption));
    if(empty($qcode) || empty($sign)){
        Result::error("参数错误");
    }
    if($encryptedString != $sign){
        Result::error("卡密异常");
    }
    $db = new Db();
    $field = 'detailed_id,add_time,state,expired_time,money,type';
    $rechargeCodeSql = "select $field from `ysmz_recharge_code_expired` where `code` = '$qcode' limit 1";
    $rechargeCode = $db->find($rechargeCodeSql);
    switch ($rechargeCode["state"]){
        case 0 : $rechargeCode["stateName"] = "未使用";break;
        case 1 : $rechargeCode["stateName"] = "已使用";break;
        case 2 : $rechargeCode["stateName"] = "已作废";break;
        default:$rechargeCode["stateName"] = "未知";break;
    }
    switch ($rechargeCode["type"]){
        case 1 : $rechargeCode["typeName"] = "云豆";break;
        case 3 : $rechargeCode["typeName"] = "生日豆";break;
        case 4 : $rechargeCode["typeName"] = "帮扶豆";break;
        default:$rechargeCode["typeName"] = "未知";break;
    }
    Result::success($rechargeCode);
}
/**
 * 扫码充值 TODO
 * @param
 * @return void
 */
function ScanRecharge($data)
{
    $qcode = $data['qcode'];
    $sign = $data['sign'];
    $encryption = "S6BZDQDNH73H";
    $user_id = Common::getUser_id($data);
    $encryptedString = MD5(MD5($qcode.$encryption));
    if(empty($qcode) || empty($sign)){
        Result::error("参数错误");
    }
    if($encryptedString != $sign){
        Result::error("验签失败");
    }
    $db = new Db("host");
    $db->startTrans();
    $field = 'detailed_id,code,user_id,add_time,state,expired_time,money,type';
    $rechargeCodeSql = "select $field from `ysmz_recharge_code_expired` where `code` = '$qcode' limit 1 FOR UPDATE";
    $rechargeCode = $db->find($rechargeCodeSql);
    if($rechargeCode["state"] != 0){
        Result::error("卡密已被激活使用或已作废");
    }
    $expired_time = strtotime($rechargeCode["expired_time"]);
    if($expired_time < time()){
        Result::error("卡密过期");
    }
    if(!empty($rechargeCode["user_id"])){
        Result::error("卡密重复使用");
    }
    $detailed_id = $rechargeCode["detailed_id"];
    $remainingMoney = $rechargeCode["money"];
    if(empty($remainingMoney) || $remainingMoney <= 0){
        Result::error("卡密金额异常，请核对");
    }
    $userInfo = $db->find("select `user_id`,`cloud_bean`,`birth_bean`,`assist_bean` from `user_info` where `user_id`=$user_id FOR UPDATE");
    switch ($rechargeCode["type"]) {//卡券类型 1云豆 3生日豆 4帮扶豆
        case 1 :
            $cloud_bean = $userInfo["cloud_bean"];
            $czSetJpin = "`cloud_bean`=`cloud_bean` + $remainingMoney";
            $czWhereJpin = "`cloud_bean` = $cloud_bean";
            $data_src = 1;
            break;
        case 3 :
            $cloud_bean = $userInfo["birth_bean"];
            $czSetJpin = "`birth_bean`=`birth_bean` + $remainingMoney";
            $czWhereJpin = "`birth_bean` = $cloud_bean";
            $data_src = 3;
            break;
        case 4 :
            $cloud_bean = $userInfo["assist_bean"];
            $czSetJpin = "`assist_bean`=`assist_bean` + $remainingMoney";
            $czWhereJpin = "`assist_bean` = $cloud_bean";
            $data_src = 4;
            break;
        default:Result::error("卡密类型错误，请核对");break;
    }
    $upRechargeCodeSql = "update `ysmz_recharge_code_expired` set `user_id` = $user_id,`state` = 1,`use_time` = NOW() where `detailed_id`=$detailed_id and `state` = 0";
    $upUserSql = "update `user_info` set $czSetJpin where `user_id`=$user_id and $czWhereJpin";
    $addOrderSql = "insert into `user_balance_detail`(user_id,balance,data_src,data_id,data_remarks,update_type,new_balance,ext_desc) values ($user_id,$remainingMoney,$data_src,'$rechargeCode[code]','扫码充值',1,$cloud_bean+$remainingMoney,'卡密充值')";
    try {
        if (!$db->save($upRechargeCodeSql)) Common::exception("充值订单修改失败");
        if (!$db->save($upUserSql)) Common::exception("用户充值余额更新失败");
        if (!$db->save($addOrderSql)) Common::exception("用户充值流水更新失败");
        $db->commit();
        Result::success("充值成功");
    } catch (\Exception $e) {
        $db->rollback();
        $content = "扫码充值失败=>支付单号：".$rechargeCode["code"].",支付金额：".$rechargeCode["money"]."原因：".$e->getMessage().",时间：".date("Y-m-d H:i:s",time());
        Utils::writeLog($content);
        Result::error($e->getMessage());
    }

}